<?php
/**
 * oscshop2 B2C电子商务系统
 *
 * ==========================================================================
 * @link      http://www.oscshop.cn/
 * @copyright Copyright (c) 2015-2017 oscshop.cn. 
 * @license   http://www.oscshop.cn/license.html License
 * ==========================================================================
 *
 * @author    李梓钿
 *
 * 快递100
 * 
 */
namespace osc\common\service;
use think\Db;
class Kuaidi{		
	
	/**
	 * 取得物流信息
	 * $order_id 订单号，$type 客户端	 * 
	 */	
	function get_trace_info($order_id,$type='pc'){
	
		$order=Db::name('order')->find($order_id);
		
		if(!$order){
			return array('error'=>'订单不存在');
		}
		
		if(empty($order['transport_c_name'])){
			return array('error'=>'快递公司不能为空');
		}
		if(empty($order['transport_num'])){
			return array('error'=>'快递单号不能为空');
		}
		
		$type_com=trim($order['transport_c_name']);
		
		if(empty($type_com)){
			return array('error'=>'快递/物流 公司代码出错');
		}
		
		$type_num=trim($order['transport_num']);		
		
		$key=config('kuaidi100_key');
		
		if($type=='mobile'){
			$url ='https://m.kuaidi100.com/index_all.html?type='.$type_com.'&postid='.$type_num.'&callbackurl='.cookie('order_info_url');
		
			return array('success'=>'查询成功','url'=>$url);
		}else{
			$url ='http://api.kuaidi100.com/applyurl?key='.$key.'&com='.$type_com.'&nu='.$type_num;
		}
		
		
		//优先使用curl模式发送数据
		if (function_exists('curl_init') == 1){
		  $curl = curl_init();
		  curl_setopt ($curl, CURLOPT_URL, $url);
		  curl_setopt ($curl, CURLOPT_HEADER,0);
		  curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
		  curl_setopt ($curl, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
		  curl_setopt ($curl, CURLOPT_TIMEOUT,5);
		  $get_content = curl_exec($curl);
		  curl_close ($curl);
		}else{
			
		  include_once EXTEND_PATH.'kuaidi/snoopy.php';
		  
		  $snoopy = new snoopy();
		  $snoopy->referer = 'http://www.google.com/';//伪装来源
		  $snoopy->fetch($url);
		  $get_content = $snoopy->results;
		}
	
		return array('success'=>'查询成功','url'=>$get_content);
		
	}	

	

}  